const http = require('../../../utils/http.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        navBar: {
            title: '收货地址',
            btn: 'goBack'
        },
        addressList: [],
        nullData: {
            img: http.imgUrl + 'null_data_1.jpg',
            txt: '暂时没有收货地址，快去添加吧~'
        }
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 到添加/编辑收货地址
     */
    goAdd(e) {
        let id = e.currentTarget.dataset.id || '0',
            consignee = e.currentTarget.dataset.consignee || '',
            telephone = e.currentTarget.dataset.telephone || '',
            province = e.currentTarget.dataset.province || '',
            city = e.currentTarget.dataset.city || '',
            county = e.currentTarget.dataset.county || '',
            place = e.currentTarget.dataset.place || '',
            isDefault = e.currentTarget.dataset.default || ''

        wx.navigateTo({
            url: '/pages/account/address/add/add?id=' + id + '&consignee=' + consignee + '&telephone=' + telephone + '&province=' + province + '&city=' + city + '&county=' + county + '&place=' + place + '&isDefault=' + isDefault,
        })
    },

    /**
     * 获取收货地址列表
     */
    getAddress(p) {
        let url = 'Ucenter_userAddress',
            token = wx.getStorageSync('token'),
            that = this

        http._post(
            url,
            { token: token, page: p },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        addressList: data.data
                    })
                } else {
                    that.setData({
                        addressList: []
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 删除地址
     */
    delAddress(e) {
        let aid = http.dataIndex(e)[1]
        this.setData({
            tipsModal: {
                show: true,
                txt: '确定删除吗？',
                cancel: 'modelHide',
                ok: 'confirmDel',
                data: {
                    aid: aid
                }
            }
        })
    },

    /**
     * 删除地址
     */
    confirmDel(e) {
        let token = wx.getStorageSync('token'),
            aid = http.dataIndex(e)[4].aid,
            url = 'Ucenter_changeAddress',
            that = this

        http._post(
            url,
            { token: token, type: 3, aid: aid },
            res => {
                // console.log(res.data)
                wx.showToast({
                    title: res.data.info,
                    icon: 'none',
                    duration: 1000
                })
                that.modelHide()
                that.getAddress(0)
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 设为默认地址
     */
    setDefault(e) {
        let aid = http.dataIndex(e)[1],
            isDefaut = e.currentTarget.dataset.defaut
        console.log(aid)
        console.log(isDefaut)
        if (isDefaut === '1') {
            this.setData({
                tipsModal: {
                    show: true,
                    txt: '确定设为默认地址吗？',
                    cancel: 'modelHide',
                    ok: 'confirmDefault',
                    data: {
                        aid: aid,
                        defaut: '2'
                    }
                }
            })
        } else {
            // this.setData({
            //     tipsModal: {
            //         show: true,
            //         txt: '当前已是默认地址',
            //         cancel: 'modelHide',
            //         ok: 'confirmDefault',
            //         data: {
            //             aid: aid,
            //             defaut: '1'
            //         }
            //     }
            // })
        }
    },

    /**
     * 确定设为默认地址
     */
    confirmDefault(e) {
        let token = wx.getStorageSync('token'),
            aid = http.dataIndex(e)[4].aid,
            defaut = e.currentTarget.dataset.defaut === "2" ? "1" : "2",
            url = 'Ucenter_changeAddress',
            that = this

        http._post(
            url,
            { token: token, type: 4, aid: aid, isDefault: defaut },
            res => {
                // console.log(res.data)
                wx.showToast({
                    title: res.data.info,
                    icon: 'none',
                    duration: 1000
                })
                that.modelHide()
                that.getAddress(0)
                wx.navigateBack({
                  url:'/pages/course/pav/pav'
                })
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 关闭提示框
     */
    modelHide() {
        this.setData({
            tipsModal: {
                show: false,
            }
        })
    },

    /**
     * 确认订单时的地址选择
     */
    chooseAddress(e) {
        let id = http.dataIndex(e)[1];
        wx.setStorageSync('aid', id)
        wx.navigateBack({
          delta: 1
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let fr = options.from || '0'
        this.setData({
            fr: fr
        })

        // this.getAddress(0)  // 获取收货地址列表
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAddress(0)  // 获取收货地址列表
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})